// pages/management/add-crop/index.js
const defaultItems = [
  {
    id: 1,
    // 导航名称
    text: "全部",
    // 是否在导航名称右上角显示小红点，1.5.0 版本开始支持
    dot: false,
    // 该导航下所有的可选项
    children: [
      // {
      //   id: 111, // 作物id
      //   classificationId: 1,
      //   classificationName: "全部", // 作物类别
      //   varieties: [
      //     {
      //       cropId: 111,
      //       cropName: "作物1",
      //       varietyName: "品种1",
      //       id: 1 // 品种id
      //     },
      //     {
      //       cropId: 111,
      //       cropName: "作物2",
      //       varietyName: "品种1",
      //       id: 2 // 品种id
      //     }
      //   ]
      // }
    ]
  }
];
Page({
	data: {
		// 默认蔬菜
    mainActiveIndex: 3,
    activeId: null,
    activeVarietyId: null,
    cropName: null,
    cropData: [],
    items: [...defaultItems],
    categoryList: [],
    varietyList: [],
    cropList: []
  },

  // 返回上一页
  toBack() {
    const { activeVarietyId, cropData } = this.data;
    if (activeVarietyId) {
      wx.setStorageSync("cropData", cropData);
      wx.navigateTo({
        url: "/pages/management/set-peel/index"
      });
    } else {
      wx.showToast({
        title: "请选择一种作物",
        icon: "none"
      });
    }
  },

  onClickNav({ detail = {} }) {
    this.setData({
      mainActiveIndex: detail.index || 0
    });
  },

  onClickItem(e) {
    const { id, varietyname, cropid, cropname } = e.currentTarget.dataset;
    let items = this.data.items.map((item) => {
      item.dot = false;
      return item;
    });
    items[this.data.mainActiveIndex].dot = true;
    this.setData({
      activeVarietyId: id,
      cropData: [
        {
          id,
          varietyName: varietyname,
          cropId: cropid,
          cropName: cropname
        }
      ],
      items
    });
  },

  getData() {
    // 获取种类
    this.getListByApplicationType().then(() => {
      // 获取品种
      this.getVarietyList().then(() => {
        this.render();
      });
    });
  },

  render() {
    let { categoryList, varietyList } = this.data;
    let items = [...categoryList];
    varietyList.forEach((val, key) => {
      let Index = items.findIndex((item) => item.id === val.classificationId);
      if (Index > -1) {
        let item = items[Index];
        item.children.push({ ...val, varieties: val.varieties || [] });
      }
    });
    this.setData({
      items
    });
  },

  // 获取种类
  getListByApplicationType() {
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: "获取中..."
      });
      wx.$apis
        .getListByApplicationType(0)
        .then((res) => {
          const { data } = res;
          this.setData({
            categoryList:
              data &&
              data.map((item) => {
                return {
                  ...item,
                  children: [],
                  dot: false,
                  text: item.name
                };
              })
          });
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },

  // 获取品种以及下面作物
  getVarietyList() {
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: "获取中..."
      });
      wx.$apis
        .getCropAndVariety({})
        .then((res) => {
          this.setData({
            varietyList: res.data || []
          });
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
